package main

import (
	"bufio"
	"fmt"
	"github.com/pion/webrtc/v3"
	"log"
	"meeting/internal/helper"
	"os"
	"strconv"
	"time"
)

func main() {
	//1.create new peer connection
	peerConnection, err := webrtc.NewPeerConnection(webrtc.Configuration{})
	if err != nil {
		return
	}
	defer func() {
		//当程序退出时要关闭 defer:栈 先进后出
		if err := peerConnection.Close(); err != nil {
			log.Println(err.Error())
		}
	}()
	//2.create data channel
	dataChannel, err := peerConnection.CreateDataChannel("foo", nil)
	dataChannel.OnOpen(func() {
		log.Println("data channel has opened")
		i := -1000
		//每隔5秒发送消息
		for range time.NewTicker(time.Second * 5).C {
			if err := dataChannel.SendText("hello world offer" + strconv.Itoa(i)); err != nil {
				log.Println(err.Error())
			}
		}
	})
	//接收来自answer的消息
	dataChannel.OnMessage(func(msg webrtc.DataChannelMessage) {
		fmt.Println(string(msg.Data))
	})
	//3.create offer
	offer, err := peerConnection.CreateOffer(nil)
	if err != nil {
		return
	}
	//4.set loacl description
	err = peerConnection.SetLocalDescription(offer)
	if err != nil {
		return
	}
	//5.print offer
	println("OFFER:")
	println(helper.Encode(offer))

	//6.input answer

	println("请输入ANSWER:")
	var answer webrtc.SessionDescription
	answerStr, _ := bufio.NewReader(os.Stdin).ReadString('\n')
	helper.Decode(answerStr, &answer)

	//7.set remote decription
	if err := peerConnection.SetRemoteDescription(answer); err != nil {
		panic(err) //
	}

	select {}
}
